home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / search.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  3KB  |  143 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "object.h"
  12.  
  13. extern F_compound    objects;
  14.  
  15. F_arc *
  16. arc_search(x, y, tolerance, px, py)
  17. int    x, y, tolerance, *px, *py;
  18. {    /*    (px, py) is the control point on the circumference of an arc
  19.         which is the closest to (x, y)                */
  20.  
  21.     F_arc    *a;
  22.     int    i;
  23.  
  24.     for (a = objects.arcs; a != NULL; a = a->next) {
  25.         for (i = 0; i < 3; i++) 
  26.         if ((abs(a->point[i].x - x) <= tolerance) && 
  27.             (abs(a->point[i].y - y) <= tolerance)) {
  28.             *px = a->point[i].x;
  29.             *py = a->point[i].y;
  30.             return(a);
  31.             }
  32.         }
  33.     return(NULL);
  34.     }
  35.  
  36.  
  37. F_ellipse *
  38. ellipse_search(x, y, tolerance, px, py)
  39. int    x, y, tolerance, *px, *py;
  40. {    /*    (px, py) is the point on the circumference of an ellipse
  41.         which is the closest to (x, y)                */
  42.  
  43.     F_ellipse    *e;
  44.     int        a, b, dx, dy;
  45.     float        dis, r, tol;
  46.  
  47.     tol = (float) tolerance;
  48.     for (e = objects.ellipses; e != NULL; e = e->next) {
  49.         dx = x - e->center.x;
  50.         dy = y - e->center.y;
  51.         a = e->radiuses.x;
  52.         b = e->radiuses.y;
  53.         dis = sqrt((double)(dx*dx + dy*dy));
  54.         r = a * b * dis / sqrt((double) (b*b*dx*dx + a*a*dy*dy));
  55.         if (fabs(dis - r) <= tol) {
  56.         *px = (int)(r*dx/dis + ((dx < 0) ? -.5 : .5)) + e->center.x;
  57.         *py = (int)(r*dy/dis + ((dy < 0) ? -.5 : .5)) + e->center.y;
  58.         return(e);
  59.         }
  60.         }
  61.     return(NULL);
  62.     }
  63.  
  64. F_line *
  65. line_search(x, y, tolerance, px, py)
  66. int    x, y, tolerance, *px, *py;
  67. {    /*    return the pointer to lines object if the search is successful
  68.         otherwise return NULL.  
  69.         The value returned via (px, py) is the closest point on the 
  70.         vector to point (x, y)                     */
  71.  
  72.     F_line    *lines;
  73.     F_point    *point;
  74.     int    x1, y1, x2, y2;
  75.     float    tol2;
  76.  
  77.     tol2 = (float) tolerance * tolerance;
  78.     for (lines = objects.lines; lines != NULL; lines = lines->next) {
  79.         point = lines->points;
  80.         x1 = point->x;
  81.         y1 = point->y;
  82.         if (abs(x - x1) <= tolerance && abs(y - y1) <= tolerance) {
  83.         *px = x1;  *py = y1;
  84.         return(lines);
  85.         }
  86.         for (point = point->next; point != NULL; point = point->next) {
  87.         x2 = point->x;
  88.         y2 = point->y;
  89.         if (close_to_vector(x1, y1, x2, y2, x, y, tolerance, tol2, 
  90.                     px, py)) 
  91.             return(lines);
  92.         x1 = x2;
  93.         y1 = y2;
  94.         }
  95.         }
  96.     return(NULL);
  97.     }
  98.  
  99. F_spline *
  100. spline_search(x, y, tolerance, px, py)
  101. int    x, y, tolerance, *px, *py;
  102. {    /*    return the pointer to lines object if the search is successful
  103.         otherwise return NULL.  */
  104.  
  105.     F_spline    *splines;
  106.     F_point        *point;
  107.     int        x1, y1, x2, y2;
  108.     float        tol2;
  109.  
  110.     tol2 = (float) tolerance * tolerance;
  111.     for (splines = objects.splines; splines != NULL; splines = splines->next) {
  112.         point = splines->points;
  113.         x1 = point->x;
  114.         y1 = point->y;
  115.         for (point = point->next; point != NULL; point = point->next) {
  116.         x2 = point->x;
  117.         y2 = point->y;
  118.         if (close_to_vector(x1, y1, x2, y2, x, y, tolerance, tol2,
  119.                     px, py)) 
  120.             return(splines);
  121.         x1 = x2;
  122.         y1 = y2;
  123.         }
  124.         }
  125.     return(NULL);
  126.     }
  127.  
  128. F_text *
  129. text_search(x, y)
  130. int    x, y;
  131. {
  132.     F_text    *t;
  133.  
  134.     for (t = objects.texts; t != NULL; t = t->next) {
  135.         if (t->base_y - t->height > y) continue;
  136.         if (t->base_y < y) continue;
  137.         if (t->base_x > x) continue;
  138.         if (t->base_x + t->length < x) continue;
  139.         return(t);
  140.         }
  141.     return(NULL);
  142.     }
  143.